package com.eonmind.pay.controller;

import com.eonmind.common.contract.data.response.ResponseEntity;
import com.eonmind.common.provider.entity.vip.VipPackage;
import com.eonmind.common.security.annotations.UserInfo;
import com.eonmind.pay.config.WxPayConfig;
import com.eonmind.pay.dto.PayDto;
import com.eonmind.pay.enums.WxApiEnum;
import com.eonmind.pay.service.PayService;
import com.eonmind.pay.utils.HttpUtils;
import com.eonmind.pay.utils.WechatPay2ValidatorForRequest;
import com.eonmind.user.contract.dto.UserInfoData;
import com.google.gson.Gson;

import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PayController
 * @Description TODO
 * Author 32004
 * Date 2025/4/28 21:28
 * Version 1.0
 **/

@Slf4j
@RestController
@RequestMapping("/pay")
public class PayController {

    @Resource
    WxPayConfig wxPayConfig;
    @Resource
    CloseableHttpClient httpClient;

    @Resource
    PayService payService;



    @GetMapping("/test")
    public ResponseEntity<String> test(){
        return ResponseEntity.success("支付接口测试成功！");
    }


    /**
     * 二维码支付接口
     *
     * @param type
     * @return
     * @ignoreParams userInfoData
     */
    @PostMapping("/pay/{type}")
    @UserInfo
    public ResponseEntity<PayDto.PayCodeDto> pay(@PathVariable("type") Integer type, UserInfoData userInfo){
        return payService.payCode(type,userInfo.getId());
    }

    /**
     * 微信支付回调接口
     *
     * @param request, response
     * @return
     * @ignoreParams userInfoData
     */
    @PostMapping("/notify")
    public String nativeNotify(HttpServletRequest request, HttpServletResponse response) {
        return payService.notifyOfWxPay(request,response);
    }

    /**
     * 根据订单id查询支付是否成功
     *
     * @param orderId
     * @return
     * @ignoreParams userInfoData
     */
    @GetMapping("/isPay")
    public ResponseEntity<Boolean> isPay(@RequestParam("orderId") String orderId){
        return payService.isPaySuccess(Long.valueOf(orderId));
    }


}
